<script>
export default {
  name: "children"
}
</script>

<template>
  <view class="container">
    <!-- 学生列表 -->
    <view class="student-list">
      <view
        v-for="student in studentList"
        :key="student.id"
        class="student-item"
      >
        <view class="student-info">
          <image :src="student.avatar || '/static/my/default_avatar.png'" class="student-avatar"></image>
          <view class="student-detail">
            <text class="student-name">{{ student.name }} {{ student.student_no }}</text>
            <text class="student-class">{{ formatGrade(student.grade) }} {{ student.class_name }}</text>
          </view>
        </view>
        <view class="student-actions">
          <button class="action-btn" @tap="viewCourses(student)">选课记录</button>
          <button class="action-btn edit-btn" @tap="editStudent(student)">解绑</button>
        </view>
      </view>

      <view class="empty-box" v-if="studentList.length === 0">
        <u-empty text="暂无学生信息"></u-empty>
      </view>
    </view>

    <!-- 底部添加按钮 -->
    <view class="footer">
      <button class="add-btn" @tap="showAddForm">添加学生</button>
    </view>

    <!-- 添加/编辑学生弹窗 -->
    <u-popup v-model="showPopup" mode="bottom" border-radius="24">
      <view class="form-content">
        <view class="popup-header">
          <text class="popup-title">{{ editingStudent ? '编辑' : '添加' }}学生</text>
          <text class="popup-close" @tap="closeForm">×</text>
        </view>

        <view class="form-body">
          <view class="form-item">
            <text class="label">年级</text>
            <picker
                class="picker"
                :range="gradeOptions"
                range-key="label"
                :value="gradeIndex"
                @change="onGradeChange"
            >
              <view class="picker-text">{{ gradeOptions[gradeIndex].label || '请选择年级' }}</view>
            </picker>
          </view>
          <view class="form-item">
            <text class="label">班级</text>
            <picker
                class="picker"
                :range="classList"
                :value="classIndex"
                @change="onClassChange"
                :disabled="!formData.grade"
            >
              <view class="picker-text">{{ classList[classIndex] || '请选择班级' }}</view>
            </picker>
          </view>
          <view class="form-item">
            <text class="label">姓名</text>
            <input
              class="input"
              v-model="formData.name"
              placeholder="请输入学生姓名"
            />
          </view>

          <view class="form-item">
            <text class="label">学号</text>
            <input
              class="input"
              v-model="formData.student_no"
              placeholder="请输入学号"
            />
          </view>
        </view>

        <view class="form-footer">
          <button class="submit-btn" @tap="submitForm">确定</button>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      studentList: [],
      showPopup: false,
      editingStudent: null,
      formData: {
        name: '',
        student_no: '',
        grade: '1',
        class_name: '',
        avatar: ''
      },
      gradeOptions: [
        { label: '一年级', value: '1' },
        { label: '二年级', value: '2' },
        { label: '三年级', value: '3' },
        { label: '四年级', value: '4' },
        { label: '五年级', value: '5' },
        { label: '六年级', value: '6' }
      ],
      classList: [],
      classIndex: 0
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    gradeIndex() {
      return this.gradeOptions.findIndex(item => item.value === this.formData.grade)
    }
  },
  onShow() {
    if (!this.userInfo || !this.userInfo.id) {
      uni.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: true,
        success: (res) => {
          if (res.confirm) {
            uni.navigateTo({
              url: '/pages/login/login'
            })
          } else if (res.cancel) {
            uni.navigateBack()
          }
        },
      })
    }
    this.getStudentList()
  },
  methods: {
    getStudentList() {
      console.log('this.userInfo',this.userInfo)
      this.$api.getChildrenList({
        parent_id: this.userInfo.id
      }).then(res => {
        this.studentList = res
      })
    },
    formatGrade(grade) {
      const gradeMap = {
        '1': '一年级',
        '2': '二年级',
        '3': '三年级',
        '4': '四年级',
        '5': '五年级',
        '6': '六年级'
      }
      return gradeMap[grade] || grade
    },
    viewCourses(student) {
      uni.navigateTo({
        url: `/pages/my/course?student_id=${student.id}`
      })
    },
    editStudent(student) {
      uni.showModal({
        title: '提示',
        content: '确定要解绑该学生吗?',
        showCancel: true,
        success: (res) => {
          if (res.confirm) {
            this.editingStudent = student
            uni.showLoading()
            this.$api.unbindStudentParent({ student_id: this.editingStudent.id}).then(() => {
              uni.showToast({
                title: this.editingStudent ? '解绑成功' : '绑定成功'
              })
              this.getStudentList()
            }).finally(() => {
              uni.hideLoading()
            })
          }
        },
      })
    },
    showAddForm() {
      this.editingStudent = null
      this.formData = {
        name: '',
        student_no: '',
        grade: '1',
        class_name: '',
        avatar: ''
      }
      this.classList = []
      this.classIndex = 0
      this.showPopup = true
      this.getClassList(1)
    },
    closeForm() {
      this.showPopup = false
      this.editingStudent = null
      this.formData = {
        name: '',
        student_no: '',
        grade: '1',
        class_name: '',
        avatar: ''
      }
      this.classList = []
      this.classIndex = 0
    },
    onGradeChange(e) {
      const index = e.detail.value
      const grade = this.gradeOptions[index].value
      this.formData.grade = grade
      this.formData.class_name = ''
      this.classIndex = 0
      this.getClassList(grade)
    },
    onClassChange(e) {
      const index = e.detail.value
      this.classIndex = index
      this.formData.class_name = this.classList[index]
    },
    getClassList(grade) {
      return this.$api.getClassList({ grade }).then(res => {
        this.classList = res.map(item => item.class_name)
        if (this.classList.length > 0) {
          this.formData.class_name = this.classList[0]
        }
      })
    },
    submitForm() {
      if (!this.formData.name) {
        uni.showToast({
          title: '请输入学生姓名',
          icon: 'none'
        })
        return
      }
      if (!this.formData.student_no) {
        uni.showToast({
          title: '请输入学号',
          icon: 'none'
        })
        return
      }
      if (!this.formData.class_name) {
        uni.showToast({
          title: '请选择班级',
          icon: 'none'
        })
        return
      }

      const params = {
        ...this.formData,
        parent_id: this.userInfo.id
      }

      const request = this.editingStudent
        ? this.$api.unbindStudentParent({ student_id: this.editingStudent.id})
        : this.$api.bindStudentParent(params)
      uni.showLoading()
      request.then(() => {
        uni.showToast({
          title: this.editingStudent ? '解绑成功' : '绑定成功'
        })
        this.closeForm()
        this.getStudentList()
      }).finally(() => {
        uni.hideLoading()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-bottom: 120rpx;
}

.student-list {
  padding: 20rpx;

  .student-item {
    background: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .student-info {
      display: flex;
      align-items: center;

      .student-avatar {
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
        margin-right: 20rpx;
      }

      .student-detail {
        .student-name {
          font-size: 32rpx;
          font-weight: bold;
          color: #333;
        }

        .student-class {
          font-size: 26rpx;
          color: #666;
          margin-top: 8rpx;
        }
      }
    }

    .student-actions {
      display: flex;
      gap: 20rpx;

      .action-btn {
        font-size: 24rpx;
        padding: 10rpx 20rpx;
        border-radius: 30rpx;
        background: #3b7ff3;
        color: #fff;

        &.edit-btn {
          background: #ff9900;
        }
      }
    }
  }
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx;
  background: #fff;
  box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);

  .add-btn {
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    background: #3b7ff3;
    color: #fff;
    border-radius: 40rpx;
    font-size: 32rpx;
  }
}

.form-content {
  background: #fff;
  padding-bottom: env(safe-area-inset-bottom);

  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid #eee;

    .popup-title {
      font-size: 32rpx;
      font-weight: bold;
    }

    .popup-close {
      font-size: 40rpx;
      color: #999;
      padding: 0 20rpx;
    }
  }

  .form-body {
    padding: 30rpx;

    .form-item {
      margin-bottom: 30rpx;

      .label {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
        display: block;
      }

      .input {
        width: 100%;
        height: 80rpx;
        border: 1rpx solid #eee;
        border-radius: 8rpx;
        padding: 0 20rpx;
        font-size: 28rpx;
      }

      .picker {
        width: 100%;
        height: 80rpx;
        border: 1rpx solid #eee;
        border-radius: 8rpx;

        .picker-text {
          line-height: 80rpx;
          padding: 0 20rpx;
          font-size: 28rpx;
        }
      }

      .upload-box {
        width: 160rpx;
        height: 160rpx;
        border: 1rpx dashed #ddd;
        border-radius: 8rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        .preview-image {
          width: 100%;
          height: 100%;
          border-radius: 8rpx;
        }

        .upload-placeholder {
          display: flex;
          flex-direction: column;
          align-items: center;

          .upload-text {
            font-size: 24rpx;
            color: #999;
            margin-top: 10rpx;
          }
        }
      }
    }
  }

  .form-footer {
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #eee;

    .submit-btn {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background: #3b7ff3;
      color: #fff;
      border-radius: 40rpx;
      font-size: 32rpx;
    }
  }
}

.empty-box {
  padding: 40rpx 0;
}
</style>
